//获取ssl状态
window.onload = function () {
    var textarea = document.getElementById("row");  
    textarea.style.height = "auto";  
    textarea.oninput = function() {  
    this.style.height = "auto";  
    this.style.height = (this.scrollHeight) + "px";  
};  
 

    //发送aiaxs请求
    $.ajax({
        url: '/ssl/GetSSL',
        type: 'POST',
        dataType: 'json',
        success: function (data) {
            
            if (data.status == false) {
                document.getElementById("ssl_loading").innerHTML = '<div class="bg-danger p-2 text-red bg-opacity-25">温馨提示:当前站点未开启SSL证书访问,站点访问可能存在风险。'+
                '<button type="button" class="btn btn-success btn-sm"'+
                'style="display: inline-block;text-align: center;" onclick="apply_cert_api()">开始申请'+
                '</button></div>'

                document.getElementById("button").innerHTML = '<button class="btn btn-success btn-sm" type="submit" onclick="setssl()" >保存证书</button>&nbsp;&nbsp;'
                document.getElementById("key").innerHTML = data.key
                document.getElementById("csr").innerHTML = data.csr
                 

    
                    

            }else if(data.httpTohttps ==true){
                document.getElementById("kg").innerHTML = `
                <div class="form-check form-switch">
                    <label class="form-check-label" for="flexSwitchCheckChecked">强制https</label>
                <input class="form-check-input" type="checkbox" role="switch" id="toggle" checked>
                
                </div>`
                document.getElementById("key").innerHTML = data.key
                    document.getElementById("csr").innerHTML = data.csr   
                    document.getElementById("button").innerHTML = '<button class="btn btn-success btn-sm" type="submit" onclick="setssl()" >保存证书</button>&nbsp;&nbsp;' +
                    '<button class="btn btn-warning btn-sm "  onclick="renew_cert()" >续签证书</button>&nbsp;' +
                    '<button class="btn btn-primary btn-sm "   onclick="CloseSSLConf()">关闭证书</button>&nbsp;'
                    document.getElementById("ssl_loading").innerHTML = '<div class="alert alert-success d-flex  opacity-53 align-items-center btn-round"  role="alert">' +
                    '证书分类:&nbspLets Encrypt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
                    '证书品牌:&nbspR3 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
                    '认证域名:&nbsp' + data.cert_data.dns + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
                    '剩余时间：&nbsp' + data.cert_data.endtime + '天 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' +
                    '到期时间: &nbsp' + data.cert_data.notAfter+'</div></div>'
                    document.getElementById("key").innerHTML = data.key
                    document.getElementById("csr").innerHTML = data.csr   
                    document.getElementById("button").innerHTML = '<button class="btn btn-success btn-sm" type="submit" onclick="setssl()" >保存证书</button>&nbsp;&nbsp;' +
                    '<button class="btn btn-warning btn-sm "  onclick="renew_cert()" >续签证书</button>&nbsp;' +
                    '<button class="btn btn-primary btn-sm "   onclick="CloseSSLConf()">关闭证书</button>&nbsp;'
                var toggle = document.getElementById('toggle');

                toggle.addEventListener('change', function () {
                    if (toggle.checked) {
                        // 开关打开时执行的代码
                        HttpToHttps()
                        
                       

                    } else {
                        // 开关关闭时执行的
                        
                        CloseToHttps()
                        
                       


                    }
                });

            }else if(data.httpTohttps ==false ){
                document.getElementById("kg").innerHTML= `
                <div class="form-check form-switch">
                    <label class="form-check-label" for="flexSwitchCheckDefault">强制https</label>
                <input class="form-check-input" type="checkbox" role="switch" id="toggle">
               
               </div>`
                document.getElementById("key").innerHTML = data.key
                    document.getElementById("csr").innerHTML = data.csr   
                    document.getElementById("button").innerHTML = '<button class="btn btn-success btn-sm" type="submit" onclick="setssl()" >保存证书</button>&nbsp;&nbsp;' +
                    '<button class="btn btn-warning btn-sm "  onclick="renew_cert()" >续签证书</button>&nbsp;' +
                    '<button class="btn btn-primary btn-sm "   onclick="CloseSSLConf()">关闭证书</button>&nbsp;'
                    document.getElementById("ssl_loading").innerHTML = '<div class="alert alert-success d-flex  opacity-53 align-items-center btn-round"  role="alert">' +
                    '证书分类:&nbspLets Encrypt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
                    '证书品牌:&nbspR3 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
                    '认证域名:&nbsp' + data.cert_data.dns + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
                    '剩余时间：&nbsp' + data.cert_data.endtime + '天 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' +
                    '到期时间: &nbsp' + data.cert_data.notAfter+'</div></div>'
                    document.getElementById("key").innerHTML = data.key
                    document.getElementById("csr").innerHTML = data.csr   
                    document.getElementById("button").innerHTML = '<button class="btn btn-success btn-sm" type="submit" onclick="setssl()" >保存证书</button>&nbsp;&nbsp;' +
                    '<button class="btn btn-warning btn-sm "  onclick="renew_cert()" >续签证书</button>&nbsp;' +
                    '<button class="btn btn-primary btn-sm "   onclick="CloseSSLConf()">关闭证书</button>&nbsp;'
                var toggle = document.getElementById('toggle');

                toggle.addEventListener('change', function () {
                    if (toggle.checked) {
                        // 开关打开时执行的代码
                        HttpToHttps()
                        

                    } else {
                        // 开关关闭时执行的
                        console.log('开关已关闭！');
                        CloseToHttps()
                        


                    }
                });

            }
            
        }
    });
          

}

// ssl 申请弹窗
function apply_cert_api(){
// 获取已绑定域名

//发送ajax请求
$.ajax({
    url: '/dns/get_domain',
    type: 'POST',
    success: function (data) {   
    // 获取下拉框的 DOM 元素  
    var select = document.getElementById('domain');  
    // 循环遍历数据，将 name 属性的值添加到下拉框中  
    for (var i = 0; i < data.length; i++) {  
        var option = document.createElement('option');  
        option.value = data[i].name;  
        option.innerHTML = data[i].name;  
        select.appendChild(option);  
    }  }
});

$.confirm({
 title: '选择域名',
 //content: 'url:form.html',  // 也可以采用url形式
 content: 
 `
 <div class="mb-3">
     <label class="form-label">选择域名</label>
     <select class="form-select" id="domain"></select>

 </div>
 <li>域名证书申请和续签限制 <a class="btlink" href="https://letsencrypt.org/zh-cn/docs/rate-limits/"target="_blank">点击查看</a></li>
 <li><span style="color:red;">Let's Encrypt因更换根证书，部分老旧设备访问时可能提示不可信</span></li>
 <li>申请之前，请确保域名已解析，如未解析会导致审核失败</li>
 <li>Let's Encrypt免费证书，有效期3个月，支持多域名。默认会自动续签</li>
 <li>若您的站点使用了CDN或301重定向会导致续签失败</li>
 <li>在未指定SSL默认站点时,未开启SSL的站点使用HTTPS会直接访问到已开启SSL的站点</li>
 <li>如开启后无法使用HTTPS访问，请检查安全组是否正确放行443端口</li>

 `,                                    
 buttons: {
     sayMyName: {
         text: '申请',
         btnClass: 'btn-orange',
         action: function() {
             var domain = document.getElementById('domain');
             {
                var l = $('body').lyearloading({
                opacity: 0.6,
                backgroundColor: '#ffffff',
                imgUrl: 'http://www.bixiaguangnian.com/home/images/loader.gif',
                spinnerText: '正在申请证书，请勿刷新页面...',
                textColorClass: 'text-info'
                

            });
            setTimeout(function() {
                l.destroy();
            }, 999999)
                 //发送ajax请求
                 $.ajax({
                     url: "/ssl/apply_cert_api",
                     method: "POST",
                     data: {
                        'domain' : domain.value,
                     },
                     success: function(data) {
                        console.log(data);
                        //保存证书
                        $.ajax({
                            url: "/ssl/setssl",
                            method: "POST",
                            data: {
                                'key' : data.private_key
                                ,
                                'csr' : data.cert,

                            },
                            success: function(data) {
                                layer.msg(data.msg,{icon: 'success'})
                                location.reload();       
                                
                            }
                        });
                        location.reload();
                       
                                            
                         
                     }
                 });
             }
         }
     },
     '关闭': function() {}
 }
});
}

//保存证书
function setssl(){
var key = document.getElementById("key").innerHTML;
var csr = document.getElementById("csr").innerHTML;
//发送ajax请求
$.ajax({
    url: "/ssl/setssl",
    method: "POST",
    data: {
        'key' : key,
        'csr' : csr,

    },
    success: function(data) {
        layer.msg(data.msg,{icon:'success'}, function(){
        //关闭后的操作
        location.reload();

        });       
        
    }
});
}
// 续签证书
function renew_cert(){
var l = $('body').lyearloading({
opacity: 0.6,
backgroundColor: '#ffffff',
imgUrl: 'http://www.bixiaguangnian.com/home/images/loader.gif',
spinnerText: '正在续签证书，请勿刷新页面...',
textColorClass: 'text-info'


});
setTimeout(function() {
    l.destroy();
}, 999999)
//发送ajax请求
$.ajax({
    url: "/ssl/GetSSL",
    method: "POST",
    success: function(data) {
        console.log(data.index)
        $.ajax({
            url: "/ssl/renew_cert",
            method: "POST",
            data: {
                'index' : data.index,
            },
            success: function(data) {
                $.confirm({
                        title: '提示',
                        content: data.msg,
                        icon: 'mdi mdi-comment-question',
                        animation: 'scale',
                        closeAnimation: 'scale',
                        opacity: 1,
                        buttons: {
                            'confirm': {
                                text: '确认',
                                btnClass: 'btn-blue',
                                action: function() {
                                    location.reload();
                                }
                            },
                            
                        }      
            }
            );
            }
        });
    }
});
}
          
// 关闭证书
function CloseSSLConf(){
//发送ajax请求
$.ajax({
    url: "/ssl/CloseSSLConf",
    method: "POST",
    success: function(data) {
        layer.msg(data.msg,{icon:'success'}, function(){
        //关闭后的操作
        
        document.getElementById("button").innerHTML = '<button class="btn btn-success btn-sm" type="submit" onclick="setssl()" >保存证书</button>&nbsp;&nbsp;'
        document.getElementById("kg").innerHTML += ''
        location.reload();

        });
        
        


       
    }
});
}
// 强制ssl开启
function CloseToHttps(){
//发送ajax请求
$.ajax({
    url: "/ssl/CloseToHttps",
    method: "POST",
    success: function(data) {
        layer.msg(data.msg,{icon:'success'}, function(){
        //关闭后的操作
        location.reload();

        });
    }
});
}
// 强制ssl关闭
function HttpToHttps(){
//发送ajax请求
$.ajax({
    url: "/ssl/HttpToHttps",
    method: "POST",
    success: function(data) {
        layer.msg(data.msg,{icon:'success'}, function(){
        //关闭后的操作
        location.reload();

        });
        
    }
});
}